
# static options
C_OS_UNKNOWN=-1
C_OS_WIN32_X86=0
C_OS_WIN32_X64=1
C_OS_LINUX_X86=2
C_OS_LINUX_X64=3
C_OS_MACOS_X86=4
C_OS_MACOS_X64=5
C_BUILD_TARGET=""

# C_OSARCH_ARRAY=("x86" "x64")
C_TAGNAME_ARRAY=("x2lib-x86" "x2lib-x64")
C_TARGET_ARRAY=("win32x86" "win32x64" "linuxx86" "linuxx64" "macosx86" "macosx64")
C_PUBLIC_ARRAY=("release" "debug")

# dynamic options
G_OPT_DEBUG_FLAG=0
G_OPT_CLEAN_FLAG=1
G_OPT_OS_TYPE=${C_OS_UNKNOWN}
G_DIR_CUR=`pwd`


# analyze all the options 
while getopts "d:c:v:" opt
do
    case $opt in
      d)
      G_OPT_DEBUG_FLAG=${OPTARG}
      ;;
      c)
      G_OPT_CLEAN_FLAG=${OPTARG}
      ;;
      v)
      G_OPT_OS_TYPE=${OPTARG}
      ;;
      ?)
      echo "parameters error!"
      exit 1
    esac
done

if [ ${G_OPT_OS_TYPE} -lt ${C_OS_WIN32_X86} ] || [ ${G_OPT_OS_TYPE} -gt ${C_OS_MACOS_X64} ]; then
    echo "parameter '-v' error! [0-Win32x86, 1-Win32x64, 2-Linuxx86, 3-Linuxx64, 4-Macosx86, 5-Macosx64]"
    exit 1
fi

G_OSARCH_NAME=${C_TARGET_ARRAY[${G_OPT_OS_TYPE}]}
G_DIR_BUILD=${G_DIR_CUR}/build-${G_OSARCH_NAME}-${C_PUBLIC_ARRAY[${G_OPT_DEBUG_FLAG}]}
G_DIR_OUTPUT=${G_DIR_CUR}/out-${G_OSARCH_NAME}

if [ ${G_OPT_DEBUG_FLAG} -eq 0 ]; then
    G_TAG_NAME=${C_TAGNAME_ARRAY[$(expr ${G_OPT_OS_TYPE} % 2)]}
else
    G_TAG_NAME=${C_TAGNAME_ARRAY[$(expr ${G_OPT_OS_TYPE} % 2)]}"-d"
fi

echo -e "\t build dir: ${G_DIR_BUILD}"
echo -e "\t out dir: ${G_DIR_OUTPUT}"
echo -e "\t debug flag: ${G_OPT_DEBUG_FLAG}"
echo -e "\t clean flag: ${G_OPT_CLEAN_FLAG}"

if [ ${G_OPT_CLEAN_FLAG} -eq 1 ]; then
    rm -rf ${G_DIR_BUILD}
    # rm -rf ${G_DIR_OUTPUT}
fi

if [ ! -x "${G_DIR_BUILD}" ]; then
    mkdir -p ${G_DIR_BUILD}
fi

if [ ! -x "${G_DIR_OUTPUT}" ]; then
    mkdir -p ${G_DIR_OUTPUT}
fi

# rm -rf "CMakeCache.txt"

cd ${G_DIR_BUILD}

if [ ${G_OPT_OS_TYPE} -eq ${C_OS_WIN32_X86} ]; then
    C_BUILD_TARGET="--target ALL_BUILD -j 6"
    cmake -S${G_DIR_CUR} -B${G_DIR_BUILD} --no-warn-unused-cli \
    -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
    -DCMAKE_BUILD_TYPE:STRING=Debug \
    -DWIN32=1 \
    -DDEBUG_OPTIONS_CMAKE=${G_OPT_DEBUG_FLAG} \
    -DDIR_OUTPUT=${G_DIR_OUTPUT} \
    -DTAG_NAME=${G_TAG_NAME} \
    -DOSARCH_NAME=${G_OSARCH_NAME} \
    -G "Visual Studio 16 2019" \
    -T host=x86 -A win32
elif [ ${G_OPT_OS_TYPE} -eq ${C_OS_WIN32_X64} ]; then
    C_BUILD_TARGET="--target ALL_BUILD -j 6"
    cmake -S${G_DIR_CUR} -B${G_DIR_BUILD} --no-warn-unused-cli \
    -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DWIN32=1 \
    -DDEBUG_OPTIONS_CMAKE=${G_OPT_DEBUG_FLAG} \
    -DDIR_OUTPUT=${G_DIR_OUTPUT} \
    -DTAG_NAME=${G_TAG_NAME} \
    -DOSARCH_NAME=${G_OSARCH_NAME} \
    -G "Visual Studio 16 2019" \
    -T host=x64 -A win32
elif [ ${G_OPT_OS_TYPE} -eq ${C_OS_LINUX_X86} ]; then
    C_BUILD_TARGET="--target all"
    cmake -S${G_DIR_CUR} -B${G_DIR_BUILD} --no-warn-unused-cli \
	-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
    -DCMAKE_BUILD_TYPE:STRING=Debug \
	-DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc \
	-DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ \
	-DUNIX=1 \
    -DLINUX=1 \
	-DDEBUG_OPTIONS_CMAKE=${G_OPT_DEBUG_FLAG} \
    -DDIR_OUTPUT=${G_DIR_OUTPUT} \
    -DTAG_NAME=${G_TAG_NAME} \
    -DOSARCH_NAME=${G_OSARCH_NAME}
elif [ ${G_OPT_OS_TYPE} -eq ${C_OS_LINUX_X64} ]; then
    C_BUILD_TARGET="--target all"
    cmake -S${G_DIR_CUR} -B${G_DIR_BUILD} --no-warn-unused-cli \
    -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc \
    -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ \
    -DUNIX=1 \
    -DLINUX=1 \
    -DDEBUG_OPTIONS_CMAKE=${G_OPT_DEBUG_FLAG} \
    -DDIR_OUTPUT=${G_DIR_OUTPUT} \
    -DTAG_NAME=${G_TAG_NAME} \
    -DOSARCH_NAME=${G_OSARCH_NAME}
elif [ ${G_OPT_OS_TYPE} -eq ${C_OS_MACOS_X86} ]; then
    C_BUILD_TARGET="--target all"
    cmake -S${G_DIR_CUR} -B${G_DIR_BUILD} --no-warn-unused-cli \
	-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
    -DCMAKE_BUILD_TYPE:STRING=Debug \
	-DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc \
	-DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ \
	-DUNIX=1 \
    -DMACOS=1 \
	-DDEBUG_OPTIONS_CMAKE=${G_OPT_DEBUG_FLAG} \
    -DDIR_OUTPUT=${G_DIR_OUTPUT} \
    -DTAG_NAME=${G_TAG_NAME} \
    -DOSARCH_NAME=${G_OSARCH_NAME}
elif [ ${G_OPT_OS_TYPE} -eq ${C_OS_MACOS_X64} ]; then
    C_BUILD_TARGET="--target all"
    cmake -S${G_DIR_CUR} -B${G_DIR_BUILD} --no-warn-unused-cli \
    -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc \
    -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ \
    -DUNIX=1 \
    -DMACOS=1 \
    -DDEBUG_OPTIONS_CMAKE=${G_OPT_DEBUG_FLAG} \
    -DDIR_OUTPUT=${G_DIR_OUTPUT} \
    -DTAG_NAME=${G_TAG_NAME} \
    -DOSARCH_NAME=${G_OSARCH_NAME}
fi

if [ ${G_OPT_DEBUG_FLAG} -eq 0 ]; then
    cmake --build . --config Release ${C_BUILD_TARGET} --
    if [ -x "${G_DIR_OUTPUT}/Release" ]; then
        mv ${G_DIR_OUTPUT}/Release/* ${G_DIR_OUTPUT}/
        rm -rf ${G_DIR_OUTPUT}/Release/
    fi
else
    cmake --build . --config Debug ${C_BUILD_TARGET} --
    if [ -x "${G_DIR_OUTPUT}/Debug" ]; then
        mv ${G_DIR_OUTPUT}/Debug/* ${G_DIR_OUTPUT}/
        rm -rf ${G_DIR_OUTPUT}/Debug/
    fi
fi

cd ${G_DIR_CUR}

